package eiisan.controller.user;

import eiisan.dao.param.user.DoLogin;
import eiisan.service.user.AuthorityService;
import eiisan.service.user.LoginService;
import eiisan.util.model.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 登录控制器
 * @author gragonfly
 * @date 2019/7/6
 **/
@RestController
@RequestMapping("/login")
@Api(tags = "登录控制器")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private AuthorityService authorityService;

    /**
     * 登录接口
     */
    @PostMapping("/auth")
    @ApiOperation(value = "登录接口")
    public Result auth(@RequestBody @ApiParam(value = "用户登录请求参数", required = true) DoLogin param) {
        return loginService.login(param);
    }

    @PostMapping(value = "/out", name = "登出接口")
    public Result out() {
        return loginService.logout();
    }

    /**
     * 测试用接口
     */
    @GetMapping("/test")
    public Result test(@RequestParam("userName") String userName) {
        return authorityService.getUserDetailByUsername(userName);
    }

    /**
     * 测试用接口2
     */
    @GetMapping("/test2")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Result test2(@RequestParam("userName") String userName) {
        return authorityService.getUserDetailByUsername(userName);
    }

    /**
     * 测试用接口2
     */
    @GetMapping("/test3")
    @PreAuthorize("hasAnyRole('ADMIN','LOGIN_TEST')")
    public Result test3(@RequestParam("userName") String userName) {
        return authorityService.getUserDetailByUsername(userName);
    }
}
